<template>
  <div class="payment">
    <div class="time_down payment_group">
      请在
      <span class="red">半小时内</span>
      完成付款，否则系统自动取消订单
    </div>

    <van-cell-group class="payment_group">
      <van-cell title="订单编号" :value="order.orderInfo.orderSn" />
      <van-cell title="实付金额">
        <span class="red">{{order.orderInfo.actualPrice *100 | yuan}}</span>
      </van-cell>
    </van-cell-group>

    <div class="pay_way_group">
      <div class="pay_way_title">选择支付方式</div>
      <van-radio-group v-model="payWay">
        <van-cell-group>
          <van-cell>
            <template slot="title">
              <img src="../../../assets/images/ali_pay.png" alt="支付宝" width="82" height="29" />
            </template>
            <van-radio name="ali" />
          </van-cell>
          <van-cell>
            <template slot="title">
              <img src="../../../assets/images/wx_pay.png" alt="微信支付" width="113" height="23" />
            </template>
            <van-radio name="wx" />
          </van-cell>
        </van-cell-group>
      </van-radio-group>
    </div>

    <van-button class="pay_submit" @click="pay" type="primary" bottomAction>去支付</van-button>
  </div>
</template>

<script>
import { Radio, RadioGroup, Dialog } from 'vant';
import { orderDetail, orderPrepay } from '@/api/api';
import _ from 'lodash';
import {Toast} from 'vant';

export default {
  name: 'payment',

  data() {
    return {
      payWay: 'wx',
        order: {
        orderInfo: {},
        orderGoods: []
      },
      orderId: 0
    };
  },
  created() {
    if (_.has(this.$route.params, 'orderId')) {
      this.orderId = this.$route.params.orderId;
      this.getOrder(this.orderId);
    }
  },
  methods: {
    getOrder(orderId) {
        orderDetail({orderId: orderId}).then(res => {
        this.order = res.data.data;
      });
    },
    pay() {
        var id = this.payWay === 'wx' ? 'wxpay' : 'alipay';
      Dialog.alert({
          message: '你选择了' + (id === 'wxpay' ? '微信支付' : '支付宝支付')
      }).then(() => {
        var that = this;
        var pays = [];
        plus.payment.getChannels(
          function(channels) {
            for (var i in channels) {
              var channel = channels[i];
              if (channel.id == 'qhpay' || channel.id == 'qihoo') {
                // 过滤掉不支持的支付通道：暂不支持360相关支付
                continue;
              }
              pays[channel.id] = channel;
                console.log(pays[channel.id] + 'channelid');
                //checkServices(channel);
            }

          },
          function(e) {

          }
        );
        // 利用weixin-js-sdk调用微信支付
          orderPrepay({orderId: this.orderId, payType: id}).then(res => {
              //res.data.data.timeStamp = parseInt(res.data.data.timeStamp);
              var order = res.data.data;
              console.log("order:" + JSON.stringify(order))
              console.log("pays:" + JSON.stringify(pays[id]))
          plus.payment.request(
              pays[id],
              order,
            function(result) {
              console.log(JSON.stringify(result));
              mui.alert('支付成功', '恭喜', function() {
                that.$router.push({
                  name: 'paymentStatus',
                  params: {
                    status: 'success'
                  }
                });
              });
            },
            function(e) {
                console.log('----- 支付失败 -----' + e.code + e.message);
            }
          );
          }).catch(() => {
          });
      });
    }
  },

  components: {
    [Radio.name]: Radio,
    [RadioGroup.name]: RadioGroup,
    [Dialog.name]: Dialog
  }
};
</script>

<style lang="scss" scoped>
.payment_group {
  margin-bottom: 10px;
}

.time_down {
  background-color: #fffeec;
  padding: 10px 15px;
}

.pay_submit {
  position: fixed;
  bottom: 0;
  width: 100%;
}

.pay_way_group img {
  vertical-align: middle;
}

.pay_way_title {
  padding: 15px;
  background-color: #fff;
}
</style>
